/**
 * 球管理类
 * Created by zhd on 2017/6/27.
 */
var BallManageLayer = cc.Layer.extend({
    _ballsSprite: [], // 球精灵集合
    _route: [], // 路径

    _route_data: [],// 路径配置数据
    _balls_data: [], // 球配置数据

    _frame: 10,


    ctor: function () {
        this._super();
        this.loadInit();
    },

    /**
     * 初始化
     */
    loadInit: function () {
        this._balls_data = g_levelInfo.balls;
        this._route_data = g_levelInfo.maps;

        this.loadRoute();
        this.schedule(this.outUpdate);
    },

    outUpdate: function () {
        var ballsSpriteLen = this._ballsSprite.length;
        var ballDataLen = this._balls_data.length;

        if (ballDataLen == 0 && ballsSpriteLen == 0) {
            // 游戏过关
        } else if (ballsSpriteLen == 0) {
            this.loadBall();
        } else if (ballDataLen == 0) {
            // 无球可出
        } else if (this._ballsSprite[ballsSpriteLen - 1]._step >= this._ballsSprite[ballsSpriteLen - 1].width) {
            this.loadBall();
        }
    },

    /**
     * 加载路径
     */
    loadRoute: function () {
        var radian = 0;// 弧度
        var rotation = 0;//度数
        var max_range = 0;// 距离

        for (var i = 0; i < this._route_data.length - 1; i++) {
            var currentPos = this._route_data[i];
            var nextPos = this._route_data[i + 1];

            radian = Math.atan2(nextPos.x - currentPos.x, nextPos.y - currentPos.y);
            rotation = 180 / Math.PI * radian;
            rotation = rotation < 0 ? 360 + rotation : rotation;

            if (nextPos.x - currentPos.x != 0) {
                max_range = (nextPos.x - currentPos.x) / Math.sin(radian);
            } else {
                max_range = Math.abs(nextPos.y - currentPos.y);
            }

            for (var j = 0; j <= max_range; j++) {
                var x = this._route_data[i].x + j * Math.sin(radian);
                var y = this._route_data[i].y + j * Math.cos(radian);
                this._route.push({x: x, y: y, r: rotation});
            }
        }
    },

    /**
     * 初始化一个球
     */
    loadBall: function () {
        var ballSprite = new BallSprite(this._balls_data.shift());
        ballSprite.setPosition(this._route[0]);
        ballSprite.roll(this._route, 1);
        this.addChild(ballSprite);
        this._ballsSprite.push(ballSprite);
    },

    /**
     * 像之前插入球(占据原来球的位置其余向后挤)
     * @param index 碰撞球下标
     * @param tag 颜色标识
     * @param pos 碰撞球位置
     */
    insertBeforeBall: function (index, tag, pos) {
        for (var i = 0; i < GC.balls.length; i++) {
            if (tag == GC.balls[i].tag) {
                break;
            }
        }

        var collideBall = this._ballsSprite[index];
        var ballSprite = new BallSprite(i);
        ballSprite.setPosition(pos);
        ballSprite._step = collideBall._step + ballSprite.width;
        ballSprite._is_roll = collideBall._is_roll;
        ballSprite._routes = collideBall._routes;
        ballSprite._step_speed = collideBall._step_speed;
        ballSprite.move(ballSprite._step, this._frame, this.check.bind(this));
        this.addChild(ballSprite);
        // 占据当前位置
        this._ballsSprite.splice(index, 0, ballSprite);


        for (var i = index - 1; i >= 0; i--) {
            // 中间空格大于一个球之后的球不向后移
            if (this._ballsSprite[i + 1]._step + this._ballsSprite[i + 1].width < this._ballsSprite[i]._step - 5) {
                break;
            }

            var mistake = this._ballsSprite[i]._step - this._ballsSprite[i + 1]._step;
            if (mistake <= 0) {
                mistake = this._ballsSprite[i].width;
            }

            this._ballsSprite[i].rollOnce(this._ballsSprite[i].width, Math.floor(mistake / this._frame));
        }
    },

    /**
     * 像之后插入球
     * @param index 碰撞球下标
     * @param tag 颜色标识
     * @param pos 碰撞球位置
     */
    insertAfterBall: function (index, tag, pos) {
        for (var i = 0; i < GC.balls.length; i++) {
            if (tag == GC.balls[i].tag) {
                break;
            }
        }
        var collideBall = this._ballsSprite[index];
        var ballSprite = new BallSprite(i);
        ballSprite.setPosition(pos);
        ballSprite._is_roll = collideBall._is_roll;
        ballSprite._routes = collideBall._routes;
        ballSprite._step_speed = collideBall._step_speed;

        var roll = false;
        if (collideBall._step >= collideBall.width) {
            if (index == this._ballsSprite.length - 1) {
                ballSprite._step = collideBall._step - ballSprite.width;
            }
            else if (this._ballsSprite[index + 1]._step + this._ballsSprite[index + 1].width < collideBall._step - 5) {
                ballSprite._step = collideBall._step - ballSprite.width;
            } else {
                ballSprite._step = collideBall._step;
                roll = true;
            }
        } else {
            ballSprite._step = collideBall._step;
            roll = true;
        }
        // 占据后面一个位置
        this._ballsSprite.splice(index + 1, 0, ballSprite);

        ballSprite.move(ballSprite._step, this._frame, this.check.bind(this));
        this.addChild(ballSprite);

        if (roll) {
            // 前面球全部向前移
            if (index + 1 != this._ballsSprite.length - 1 || collideBall._step < collideBall.width) {
                for (var i = index; i >= 0; i--) {
                    // 中间空格大于一个球之后的球不向前移
                    if (this._ballsSprite[i + 1]._step + this._ballsSprite[i + 1].width < this._ballsSprite[i]._step - 5) {
                        break;
                    }

                    var mistake = this._ballsSprite[i]._step - this._ballsSprite[i + 1]._step;
                    if (mistake <= 0) {
                        mistake = this._ballsSprite[i].width;
                    }

                    this._ballsSprite[i].rollOnce(this._ballsSprite[i].width, Math.floor(mistake / this._frame));
                }
            }
        }


    },

    /**
     * 检查
     */
    check: function () {
        this.checkSame();
    },

    /**
     * 检查3个及以上相同的，并消失(必须是靠近的)
     */
    checkSame: function () {
        // cc.log(this._ballsSprite);
        var length = this._ballsSprite.length;
        var clear_arr = [this._ballsSprite[0]];
        var c_len = 0;
        for (var i = 1; i < length; i++) {
            cc.log(this._ballsSprite[i - 1]._step + "  " + (this._ballsSprite[i]._step + this._ballsSprite[i].width));

            c_len = clear_arr.length;
            if (this._ballsSprite[i - 1]._tag == this._ballsSprite[i]._tag
                && this._ballsSprite[i - 1]._step <= this._ballsSprite[i]._step + this._ballsSprite[i].width) {
                clear_arr.push(this._ballsSprite[i]);
            } else {
                if (clear_arr.length >= 3) {
                    break;
                }
                clear_arr = [this._ballsSprite[i]];
            }
        }

        c_len = clear_arr.length;
        if (c_len >= 3) {
            this._ballsSprite.splice(i - c_len, c_len);
            for (var j = 0; j < c_len; j++) {
                this.removeChild(clear_arr[j]);
            }
        }
    }


});